
#include "HashCommon.h"

unsigned long CHashCommon::hashString( const char *lpszFileName, unsigned long dwHashType )
{
	unsigned char *key = (unsigned char *)lpszFileName;
	unsigned long seed1 = 0x7FED7FED;
	unsigned long seed2 = 0xEEEEEEEE;
	int ch;
	while ( 0 != *key)
	{
		ch = toupper( *key++);
		seed1 = m_decryptTable[(dwHashType<<8)+ch]^(seed1 + seed2);
		seed2 = ch + seed1 + seed2 + (seed2<<5) + 3;
	}
	return seed1;
}


bool CHashCommon::hashFileName( const char *pbKey, SFileHashValue & hashValue )
{
	hashValue.m_iHashIndex = hashString( pbKey, 0);
	hashValue.m_iName1     = hashString( pbKey, 1);
	hashValue.m_iName2     = hashString( pbKey, 2);
	return true;
}


void CHashCommon::_calDecryptTable()
{
	unsigned long seed = 0x00100001, index1 = 0, index2 = 0, i;
	for( index1 = 0; index1 < 0x100; index1++)
	{
		for(index2 = index1, i=0; i<5; i++, index2 += 0x100)
		{
			unsigned long temp1, temp2;
			seed = (seed * 125 + 3)%0x2AAAAB;
			temp1 = (seed & 0xFFFF)<<0x10;
			seed = (seed * 125 + 3) % 0x2AAAAB;
			temp2 = (seed & 0xFFFF);
			m_decryptTable[index2] = (temp1|temp2 );
		}
	}
}